#include <mimalloc.h>
#include <utl/allocator.h>
#include <Windows.h>
#include <common_header.h>

namespace utl
{
	/*struct HEAP_DELETER
	{
		void operator()(mi_heap_t* ptr)const noexcept
		{
			mi_heap_destroy(ptr);
		}
	};

	using heap_ptr = eastl::unique_ptr<mi_heap_t, HEAP_DELETER>;

	static heap_ptr& get_heap_ptr()
	{
		static heap_ptr hp{ mi_heap_new() };
		return hp;
	}*/

	void* allocator::allocate(unsigned long n, int flags)
	{
		return mi_malloc(n);
	}

	void* allocator::allocate(unsigned long n, unsigned long alignment, unsigned long offset, int flags)
	{
		return mi_malloc(n);
	}

	void  allocator::deallocate(void* p, unsigned long n)
	{
		mi_free(p);
	}

	bool operator==(const allocator& a, const allocator& b)
	{
		return true;
	}

	/*bool operator==(const tls_allocator& a, const tls_allocator& b)
	{
		return true;
	}*/

	bool operator!=(const allocator& a, const allocator& b)
	{
		return false;
	}

	//bool operator!=(const tls_allocator& a, const tls_allocator& b)
	//{
	//	return false;
	//}


	//tls_allocator::tls_allocator(const char* pName /*= "utl"*/) :
	//	heap_ptr{ ((TLS_HAEP*)TlsGetValue(tls_mappint()[TLS_TYPE_STACK_HEAP])) }
	//{

	//}


	//tls_allocator::tls_allocator(const allocator& x) :
	//	heap_ptr{ ((TLS_HAEP*)TlsGetValue(tls_mappint()[TLS_TYPE_STACK_HEAP])) }
	//{

	//}


	//tls_allocator::tls_allocator(const allocator& x, const char* pName) :
	//	heap_ptr{ ((TLS_HAEP*)TlsGetValue(tls_mappint()[TLS_TYPE_STACK_HEAP])) }
	//{

	//}

	//void* tls_allocator::allocate(unsigned long n, int flags /*= 0*/)
	//{
	//	void* ptr = &heap_ptr->storage[heap_ptr->offset];
	//	heap_ptr->offset += n;
	//	return ptr;
	//}


	//void* tls_allocator::allocate(unsigned long n, unsigned long alignment, unsigned long offset, int flags /*= 0*/)
	//{
	//	void* ptr = &heap_ptr->storage[heap_ptr->offset];
	//	heap_ptr->offset += n;
	//	return ptr;
	//}


	//void tls_allocator::deallocate(void* p, unsigned long n /*= 0*/)
	//{

	//}


	//tls_start::tls_start() :
	//	start_addr{ ((TLS_HAEP*)TlsGetValue(tls_mappint()[TLS_TYPE_STACK_HEAP]))->offset}
	//{
	//}

	//tls_start::~tls_start()
	//{
	//	((TLS_HAEP*)TlsGetValue(tls_mappint()[TLS_TYPE_STACK_HEAP]))->offset = start_addr;
	//}

}